package io.renren.config;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author: xiaomi
 * @date: 2021/8/31
 * @description:
 */
@Configuration
@EnableConfigurationProperties(ThreadPoolConfigProperties.class)
public class ThreadPoolConfig {


    @Bean("calcPool")
    public ThreadPoolExecutor calcPool(ThreadPoolConfigProperties config) {
        return new ThreadPoolExecutor(config.getCoreSize(), config.getMaxSize(), config.getKeepAliveTime(),
                TimeUnit.SECONDS, new LinkedBlockingDeque<>(100000),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
    }

    @Bean(BizConfig.POOL_FFMPEG)
    public ThreadPoolExecutor ffmpegPool(ThreadPoolConfigProperties config) {
        return new ThreadPoolExecutor(config.getCoreSize(), config.getMaxSize(), config.getKeepAliveTime(),
                TimeUnit.SECONDS, new LinkedBlockingDeque<>(100000),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
    }

    /**
     * 时间轮线程池
     *
     * @return
     */
    @Bean(BizConfig.POOL_TIME_WHEEL)
    public ThreadPoolExecutor timeWheelThreadPoolExecutor() {
        return new ThreadPoolExecutor(5, 5, 60,
                TimeUnit.SECONDS, new LinkedBlockingDeque<>(1000),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
    }
}
